//
//  GWPrivacyView.m
//  ShandsMod
//
//  Created by 熊鑫 on 2022/5/7.
//  Copyright © 2022 zh_s. All rights reserved.
//

#import "GWPrivacyView.h"
#import "YYText.h"

@implementation GWPrivacyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    
    NSString *PrivacyStr = @"我已仔细阅读并同意《使用者协议》";
    //_yyLabel = [[YYLabel alloc]initWithFrame:CGRectMake(22, 0, 160, 13)];
    _yyLabel = [[YYLabel alloc] init];
    _yyLabel.text = PrivacyStr;
    _yyLabel.font = FONT_NAME(FONT_Regular, 12);
    _yyLabel.textColor = RGB(175, 182, 203);
    [self addSubview:_yyLabel];
    
    NSMutableAttributedString* atext=[[NSMutableAttributedString alloc]initWithString:PrivacyStr];
    //设置下划线
    NSRange range=[[atext string]rangeOfString:@"《使用者协议》" ];
    YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleNone) width:[NSNumber numberWithInt:1] color:RGB(98, 123, 242)];
    [atext yy_setTextUnderline:deco range:range];
    //设置高亮色和点击事件
    WS(weakSelf);
    [atext yy_setTextHighlightRange:range color:RGB(98, 123, 242) backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSString* str=text.string;
        NSLog(@"你点击了 %@ ---range %ld", [str substringWithRange:range] ,range.length);
        weakSelf.privacyViewBlock();
    }];
    
    NSRange range1=[[atext string]rangeOfString:@"我已仔细阅读并同意" ];
    [atext yy_setTextHighlightRange:range1 color:RGB(175, 182, 203) backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [self selectedBtnClick];
    }];
    _yyLabel.attributedText = atext;  //设置富文本
    [_yyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).mas_offset(-10);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(16);
        make.width.mas_equalTo(192);
    }];

    _selectedBtn = [BaseViewFactory buttonWithWidth:16 imagePath:@"icon_walletList_nol"];
    [_selectedBtn setImage:IMAGE_NAME(@"icon_walletList_sel") forState:UIControlStateSelected];
    [self addSubview:_selectedBtn];
//    _selectedBtn.frame = CGRectMake(0, self.height/2 - 8, 16, 16);
    [_selectedBtn addTarget:self action:@selector(selectedBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_selectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_yyLabel.mas_left).mas_offset(-5);
        make.centerY.mas_equalTo(_yyLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
}


- (void)selectedBtnClick{
    _selectedBtn.selected = !_selectedBtn.selected;
    
}

@end
